﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Kristall.Screens;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Kristall.Engine;
using Kristall.Editor.Commands;

namespace Kristall
{
    public class RectangleTool : Tool
    {
        #region Properties And Fields
        public Rectangle Rectangle = Rectangle.Empty;

        public override string CursorTexture
        {
            get { return "Rectangle"; }
        }

        public override Keys Hotkey
        {
            get { return Keys.D1; }
        }

        public override Vector2 CursorOffset
        {
            get
            {
                return new Vector2(0, 32);
            }
        }

        Vector2 mouseDownPosition;

        #endregion

        public RectangleTool(LevelEditor editor)
            : base(editor)
        {

        }

        public override void Reset()
        {
            base.Reset();
            Rectangle = Rectangle.Empty;
        }

        public override void MouseDown(Vector2 mousePosition)
        {
            base.MouseDown(mousePosition);

            mouseDownPosition = mousePosition;
        }

        public override void MouseUp(Vector2 mousePosition)
        {
            if (Rectangle.Width >= 0 && Rectangle.Height > 0)
            {
                Animatable temp = new Animatable();
                temp.SetSize(Rectangle.Width, Rectangle.Height);
                temp.Position = new Vector2(Rectangle.X, Rectangle.Y);
                temp.LoadContent(LevelEditor.Level);
                LevelEditor.ToolPalette.CommandManager.ExecuteCommand(new AddToLevelCommand(temp));
                Rectangle = Rectangle.Empty;
            }

            base.MouseUp(mousePosition);
        }

        public override void Update(float dt)
        {
            if (IsMouseDown)
            {
                Rectangle r1 = new Rectangle((int)MousePositionWorld.X, (int)MousePositionWorld.Y, 0, 0);
                Rectangle r2 = new Rectangle((int)mouseDownPosition.X, (int)mouseDownPosition.Y, 0, 0);

                Rectangle.Union(ref r1, ref r2, out Rectangle);

                /*int width = (int)(MousePosition.X - Rectangle.X);
                int height = (int)(MousePosition.Y - Rectangle.Y);

                if (width < 0)
                {

                }

                if (height < 0)
                {

                }

                Rectangle.Width = width;
                Rectangle.Height = height;*/
            }
        }


        public override void Draw(SpriteBatch batch)
        {
            base.Draw(batch);
            batch.Draw(Kristall.Blank, Rectangle, Color.White * 0.66f);
        }
    }

}
